package com.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @author 王霄
 * @date 2022/1/8 13:07
 */
@Configuration
public class HeadersConfig {

    public static final String EXCHANGE_NAME = "headers.exchange";
    public static final String QUEUE1 = "headers.queue1";
    public static final String QUEUE2 = "headers.queue2";
    public static final String QUEUE3 = "headers.queue3";
    public static final String QUEUE4 = "headers.queue4";
    public static final String QUEUE5 = "headers.queue5";
    public static final String QUEUE6 = "headers.queue6";

    @Bean(EXCHANGE_NAME)
    public HeadersExchange headersExchange() {
        return new HeadersExchange(EXCHANGE_NAME);
    }

    @Bean(QUEUE1)
    public Queue queue1() {
        return new Queue(QUEUE1);
    }

    @Bean(QUEUE2)
    public Queue queue2() {
        return new Queue(QUEUE2);
    }

    @Bean(QUEUE3)
    public Queue queue3() {
        return new Queue(QUEUE3);
    }

    @Bean(QUEUE4)
    public Queue queue4() {
        return new Queue(QUEUE4);
    }

    @Bean(QUEUE5)
    public Queue queue5() {
        return new Queue(QUEUE5);
    }

    @Bean(QUEUE6)
    public Queue queue6() {
        return new Queue(QUEUE6);
    }

    @Bean
    public Binding topicBinding1() {
        Map<String, Object> headers = Map.of("one", 1, "two", 2);
        return BindingBuilder
                .bind(queue1())
                .to(headersExchange())
                .whereAll(headers)
                .match();
    }

    @Bean
    public Binding topicBinding2() {
        Map<String, Object> headers = Map.of("one", 1, "two", 2);
        return BindingBuilder
                .bind(queue2())
                .to(headersExchange())
                .whereAny(headers)
                .match();
    }

    @Bean
    public Binding topicBinding3() {
        return BindingBuilder
                .bind(queue3())
                .to(headersExchange())
                .whereAll("one", "two")
                .exist();
    }

    @Bean
    public Binding topicBinding4() {
        return BindingBuilder
                .bind(queue4())
                .to(headersExchange())
                .whereAny("one", "two")
                .exist();
    }

    @Bean
    public Binding topicBinding5() {
        return BindingBuilder
                .bind(queue5())
                .to(headersExchange())
                .where("one")
                .exists();
    }

    @Bean
    public Binding topicBinding6() {
        return BindingBuilder
                .bind(queue6())
                .to(headersExchange())
                .where("one")
                .matches(100);
    }

}
